home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 9825 / 9825.xpi / chrome / content / linkify.js < prev    next >
Text File  |  2010-01-13  |  6KB  |  180 lines

  1. //Note to editor: NOT executed from XUL
  2. //Copyright 2009 Yongqian Li.
  3. (function()
  4. {
  5.     //var $ = smarterwiki_$;
  6.     
  7.     var linkify = function(doc)
  8.     {
  9.         var excludedTags = "a,applet,area,cite,embed,frame,frameset,head,iframe,img,map,meta,noscript,object,option,param,pre,script,select,style,textarea,title,*[@onclick],*[@onmousedown],*[@onmouseup],*[@tiddler],*[@class='linkification-disabled']".split(",");
  10.         //"cite" because of google
  11.         //"pre" because of http://www.tiddlywiki.com/
  12.         var xpath = "//text()[not(ancestor::" + excludedTags.join(') and not(ancestor::') + ")]";
  13.         var result = doc.evaluate(xpath, doc, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); 
  14.         var url_regexp = new RegExp("(((https?|ftp|irc|file)://|www\\.)[^<>'\"\\s]*[^<>)'\"\\s,.])|([^[\\]<>'\"\\s]+@[^<>'\"\\s]+\\.[^<>'\"\\s]+)", "gi");
  15.  
  16.         for(var i = 0; i < result.snapshotLength; i++)
  17.         {
  18.             var candidate = result.snapshotItem(i);
  19.             if(candidate.nodeName == "#cdata-section")
  20.             {
  21.                 continue;//ignore cdata
  22.             }
  23.             var text = candidate.textContent;
  24.             var lastLastIndex = 0;
  25.             var match = url_regexp.exec(text);
  26.             if(match)
  27.             {
  28.                 var span = doc.createElement("span");
  29.                 while(match)
  30.                 {
  31.                     span.appendChild(doc.createTextNode(text.substring(lastLastIndex, match.index)));
  32.  
  33.                     var url = match[0];
  34.                     if(url.indexOf("www.") == 0) {
  35.                         url = "http://" + url;
  36.                     }
  37.                     else if(url.indexOf("://") == -1 && url.indexOf('@') != -1) {
  38.                         url = "mailto:" + url;
  39.                     }
  40.                     
  41.                     var a = doc.createElement("a");
  42.                     a.className = "smarterwiki-linkify";
  43.                     a.setAttribute("href", url);
  44.                     /*
  45.                     $(a, doc)
  46.                         .css("color", $(candidate.parentNode, doc).css("color"))
  47.                         .css("background-color", $(candidate.parentNode, doc).css("background-color"))
  48.                         .css("text-decoration", $(candidate.parentNode, doc).css("text-decoration"));
  49.                     */
  50.                     a.appendChild(doc.createTextNode(match[0]));
  51.  
  52.                     span.appendChild(a);
  53.                     lastLastIndex = url_regexp.lastIndex;
  54.  
  55.                     match = url_regexp.exec(text);
  56.                 }
  57.  
  58.                 span.appendChild(doc.createTextNode(text.substring(lastLastIndex)));
  59.                 candidate.parentNode.replaceChild(span, candidate);
  60.             }
  61.         }
  62.     };
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.     var safe_SW_getBoolPref = function(name)
  75.     {
  76.         try
  77.         {
  78.             return SW_getBoolPref(name);
  79.         }
  80.         catch(err)
  81.         {
  82.             return null;
  83.         }
  84.     };
  85.  
  86.     var getBoolPref = function(pref_name, callback)
  87.     {
  88.         if(callback)
  89.         {
  90.             callback(safe_SW_getBoolPref("extensions.smarterwiki." + pref_name));
  91.         }
  92.     };
  93.     if(window.chrome && window.chrome.extension) // detect chrome
  94.     {
  95.         $.prototype.ready = function(callback) {
  96.             callback();
  97.         };//doesn't work on Chrome, cheap hack
  98.  
  99.         var get_localStorage = function(key, callback) //only used for Chrome
  100.         {    
  101.             chrome.extension.sendRequest({"msg_type": "get_localStorage", "key": key}, function(response)
  102.             {
  103.                 if(callback)
  104.                 {
  105.                     callback(response.value);
  106.                 }
  107.             });
  108.         };
  109.         var set_localStorage = function(key, value, callback) //only used for Chrome
  110.         {    
  111.             chrome.extension.sendRequest({"msg_type": "set_localStorage", "key": key, "value": value}, function(response)
  112.             {
  113.                 if(callback)
  114.                 {
  115.                     callback(response.status);
  116.                 }
  117.             });
  118.         };
  119.  
  120.         setBoolPref = function(pref_name, value, callback)
  121.         {
  122.             set_localStorage("pref." + pref_name, value, callback);
  123.         };
  124.  
  125.         getBoolPref = function(pref_name, callback)
  126.         {
  127.             get_localStorage("pref." + pref_name, function(str)
  128.             {
  129.                 if(callback) {
  130.                     callback(str == "true");
  131.                 }
  132.             });
  133.         };
  134.         
  135.         SW_$ = $;
  136.         SW_LOG = function(){};
  137.         $.get = function(url, data, callback, type)
  138.         {
  139.             chrome.extension.sendRequest({"msg_type": "$.get", "url": url, "data": data, "type": type}, function(response)
  140.             {
  141.                 if(callback)
  142.                 {
  143.                     callback(response.data, response.textStatus);
  144.                 }
  145.             });            
  146.         };
  147.     }
  148.  
  149.     $(document).ready(function()
  150.     {
  151.         if(document.designMode == "off")
  152.         {
  153.             getBoolPref("enable_linkify", function(pref_value)
  154.             {
  155.                 if(pref_value)
  156.                 {
  157.                     //if(document.documentElement.namespaceURI == null) doesn't work on chrome
  158.                     var blacklisted_domains = ["www.guardian.co.uk", /surfcanyon.com/];
  159.                     for(var i = 0; i < blacklisted_domains.length; i++) {
  160.                         if(blacklisted_domains[i] == window.location.host) {
  161.                             return;
  162.                         }
  163.                         if(blacklisted_domains[i].test && blacklisted_domains[i].test(window.location))
  164.                         {
  165.                             return;
  166.                         }
  167.                     }
  168.                     setTimeout(function()
  169.                     {
  170.                         linkify(document);
  171.                     }, 200);
  172.                 }
  173.             });
  174.         }
  175.         else
  176.         {
  177.             // alert("in rich editor");
  178.         }
  179.     });
  180. }());